#include "GPA.h"

using namespace std;


int main(void)
{
	int MaxNumStu,	// Record upper bound
		NumStu;		// number of records

	StudentRec_t * student; // array of records

	MaxNumStu = GetMaxNumStu();

	student = new StudentRec_t[MaxNumStu];

	if (student == NULL)
	{
		cerr << "Cannot allocate space for array.";
		exit(1);
	}

	GetData(student, NumStu, MaxNumStu);

	cout << endl << "Report of GRaduates\n\n";
	PrintReport(student, NumStu, true);

	cout << endl << "Report of Under-GRaduates\n\n";
	PrintReport(student, NumStu, false);

	return 0;
}

int GetMaxNumStu(void)
{
	int MaxNumStu;

	cout << "GIve a max number of records to be processed.";
	cin >> MaxNumStu;

	return MaxNumStu;
}

void GetData(StudentRec_t* student, int& NumStu, const int MaxNumStu)
{
	char	cont,
			GradAns;

	NumStu = 0;

	do
	{
		cout << "Has student graduated? ";
		cin >> GradAns;

		student[NumStu].grad = toupper(GradAns) == 'Y' ? true : false;

		cout << "Quality points:";
		cin >> student[NumStu].QualityPoints;
		cout << "Hours attempted";
		cin >> student[NumStu].HoursAttempted;

		NumStu++;

		cout << "Do you want to add another record?";
		cin >> cont;

	} while (toupper(cont) == 'Y' && NumStu < MaxNumStu);
}

void PrintReport(const StudentRec_t * student, const int NumStu, const bool GradValue)
{
	int i = 0;
	float avg;

	while (i < NumStu)
	{
		if (student[i].grad == GradValue)
		{
			if (student[i].HoursAttempted > 0)
				avg = float(student[i].QualityPoints);

			else
				avg = 0.0;
			cout << "GPA for student " << i << " is " << avg << endl;
		}
		i++;
	}
}